package coms.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import coms.datatransfer.ConferenceDTO;
import coms.datatransfer.ConferenceItemDTO;
import coms.datatransfer.ConferenceItem_User;
import coms.datatransfer.FileItemDTO;
import coms.datatransfer.UserDTO;
import coms.ejb.sessionbean.ConferenceBeanRemote;
import coms.ejb.sessionbean.FileBeanRemote;
import coms.service.BusinessDelegate;

import java.util.*;

public class ConferenceAction extends ActionSupport implements ModelDriven<ConferenceDTO>{
	
	private static final long serialVersionUID = 1L;
	private ConferenceDTO conference = new ConferenceDTO();
	private BusinessDelegate delegate;
	private ArrayList<ConferenceDTO> conferenceList;
	private ConferenceBeanRemote conferenceBean;
	private FileBeanRemote fileBean;
	
	public ConferenceAction(){
		delegate = new BusinessDelegate();
		conferenceBean = (ConferenceBeanRemote)delegate.getBean("conferenceBean");
		fileBean = (FileBeanRemote)delegate.getBean("fileBean");
	}
	
	@Override
    public ConferenceDTO getModel() {
		return conference;
	}
   
	public String saveOrUpdate() {
		try{
		System.out.println("list of conference items: " + conference.getListOfConferenceItems());
		Map<String,Object> session = ActionContext.getContext().getSession();
		
		List<ConferenceItemDTO> conferenceItemsList = new ArrayList<ConferenceItemDTO>();
		conference.setListOfConferenceItems(conferenceItemsList);
		
		Integer createConfId = conferenceBean.createConference(conference);
		
		session.put("createConfId", createConfId);
		return "success";
		} catch (Exception e){
			return "exception";
		}
    }
    
	public String execute() throws Exception {		
		return "success";
	}
	
	public String createConferenceDisplay() throws Exception {		
		return "success";
	}	
	
	public String displayAll() throws Exception {
		return "display";
	}
	
	
	public String displayConferenceList() throws Exception {		
		conferenceList =(ArrayList<ConferenceDTO>) conferenceBean.findAllConferences();		 
		return "display";
	}
	
     public String createConference() {
    	 try{
    	Map<String,Object> session = ActionContext.getContext().getSession();
    	if(session.get("createConfId") != null){
    		session.put("createConfId", null);
    	}
		return "success";
    	 } catch (Exception e){
    		 return "exception";
    	 }
	}
	
	public void createFileItem() {
		FileItemDTO fileItemDTO = new FileItemDTO();
		
		fileItemDTO.setAuthorID(1);
		fileItemDTO.setCategory("newcat2");
		fileItemDTO.setFileName("file4");
		fileItemDTO.setLocation("filelocation");
		//fileItemDTO.setConferenceItemID(1);
		
		UserDTO userDTO1 = new UserDTO();
		userDTO1.setUserID(5);
		userDTO1.setFirstname("reviewer1FirstName");
		userDTO1.setLastname("reviewer1LastName");
		userDTO1.setProfile("userProfile");
		
		userDTO1.setPassword("gayathri");
		
		UserDTO userDTO2 = new UserDTO();
		userDTO2.setUserID(6);
		userDTO2.setFirstname("reviewer1FirstName");
		userDTO2.setLastname("reviewer1LastName");
		userDTO2.setProfile("userProfile");
		
		userDTO2.setPassword("gayathri");
		
		Set<UserDTO> listUser = new HashSet<UserDTO>();
		listUser.add(userDTO1);
		listUser.add(userDTO2);
		
		fileItemDTO.setListOfReviewers(listUser);
		
		fileBean.insertFileItem(fileItemDTO);			
	}
	
	public ConferenceDTO getConference() {
		return conference;
	}

	public void setConference(ConferenceDTO conference) {
		this.conference = conference;
	}
	
	public ArrayList<ConferenceDTO> getConferenceList() {
		return conferenceList;
	}

	public void setConferenceList(ArrayList<ConferenceDTO> conferenceList) {
		this.conferenceList = conferenceList;
	}
	
	/*public void validate(){
		if(conference.getConferenceName().length() == 0){
			addFieldError("conferenceName","Name of conference is required");
		}
		if((conference.getConferenceStartDate()).toString().length() == 0){
			addFieldError("conferenceStartDate","Start date is required");
		}
		if((conference.getConferenceEndDate()).toString().length() == 0){
			addFieldError("conferenceEndDate","End date is required");
		}
		if(conference.getLocation().length() == 0){
			addFieldError("location","Location is required");
		}
		if(conference.getEvent().length() == 0){
			addFieldError("event","Event is required");
		}
		if((conference.getReviewTimeline()).toString().length() == 0){
			addFieldError("reviewTimeLine","Review time is required");
		}
	}*/
}
